﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Midi;

namespace MIDIRemapper.DeviceInterfaces
{
    partial class DeviceInterface
    {
        private Dictionary<MidiType, MidiType> globalMatches
            = new Dictionary<MidiType, MidiType>();

        /// <summary>
        /// Add a global transformation.
        /// </summary>
        /// <param name="t_in">MIDIType of input midi message</param>
        /// <param name="t_out">MIDIType of output midi message</param>
        public void AddGlobal(MidiType t_in, MidiType t_out)
        {
            globalMatches.Add(t_in, t_out);
        }

        public void RemoveGlobal(int i)
        {
            globalMatches.Remove(globalMatches.Keys.ElementAt(i));
        }

        public void LoadGlobal(Dictionary<MidiType, MidiType> transformations)
        {
            globalMatches = transformations;
        }
        
        /// <summary>
        /// Looks for a match for the given MIDIType, returns it via out parameter
        /// </summary>
        /// <param name="t_in">Input type</param>
        /// <param name="t_out">Sets the output type associated with the input</param>
        /// <returns>Match found</returns>
        bool GetGlobalMatch(MidiType t_in, out MidiType t_out)
        {
            return globalMatches.TryGetValue(t_in, out t_out);
        }

        public Dictionary<MidiType, MidiType> GetGlobals()
        {
            return globalMatches;
        }

        private void AddGlobal(KeyValuePair<MidiType, MidiType> route)
        {
            globalMatches.Add(route.Key, route.Value);
        }
    }
}
